// components/hope/index.js

var paginationBev = require('../behaviors/pagination.js')
var HTTP = require('../../utils/http.js')
import { appendImageUrl } from '../../utils/util.js'
let app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  behaviors: [paginationBev],
  properties: {
    more: {
      type: String,
      observer: '_loadMore'
    },
    refresh:{
      type: String,
      observer: 'request'
    },
    isHope: {
      type: "Number",
      value: Number,
      observer: function (news, olds, path) {
        
      }
    }
  },
  

  /**
   * 组件的初始数据
   */
  data: {
    loading: false,
    loadingCenter: false
  },
  

  // attached: function () {
    // debugger
    // this.request()
  // },

  // ready: function () {
  //   this.request()
  // },

  /**
   * 组件的方法列表
   */
  methods: {
    request: function () {
      
      wx.showLoading({
        title: '加载中',
      })
      this.setData({
        loading: false
      })
      this.initPagination()

      const h = HTTP.request({
        url: (this.properties.isHope == 1) ? '/miniapp/showCar/queryMyWishList' : '/miniapp/showCar/queryMyLikeList',
        data: {
          token: getApp().globalData.loginInfo.token,
          isPaging: '1',
          curPage: this.getCurrentStart()
        }
      })
      h.then((res) => {
        res.data.forEach(v => {
          v.totalListPicture = appendImageUrl(v.listPicture)
        })

        this.setMoreData(res.data)
        wx.hideLoading()
        if (!this.hasMore()) {
          this.setData({
            loading: false
          })
        } else {
          this.setData({
            loading: true
          })
        }
        wx.stopPullDownRefresh()
      }, (error) => {
        wx.hideLoading()
        wx.stopPullDownRefresh()
      })
    },

    _loadMore: function () {
      
      if (this.isFirstStart()) {
        return;
      }
      if (!this.hasMore()) {
        this.setData({
          loading: false
        })
        return
      }
      this.setData({
        loading: true
      })
      
      const h = HTTP.request({
        url: (this.properties.isHope == 1) ? '/miniapp/showCar/queryMyWishList' : '/miniapp/showCar/queryMyLikeList',
        data: {
          token: getApp().globalData.loginInfo.token,
          isPaging: '1',
          curPage: this.getCurrentStart()
        }
      })
      h.then((res) => {
        res.data.forEach(v => {
          v.totalListPicture = appendImageUrl(v.listPicture)
        })
        this.setMoreData(res.data)
        if (!this.hasMore()) {
          this.setData({
            loading: false
          })
        }
      }, (error) => {
        
      })
    },

    onJumpToCarDetail: function (event) {
      wx.navigateTo({
        url: '/pages/carDetail/carDetail?bid=' + event.currentTarget.id,  
      })
    }
  }
})
